package com.caifu.service.impl;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caifu.mapper.SysShortMessageMapper;
import com.caifu.pojo.SysShortMessage;
import com.caifu.service.SysShortMessageService;
import com.caifu.util.Util;

import net.sf.json.JSONObject;
/**
 * <p>
 * 短信表 服务实现类
 * </p>
 *
 * @author szt
 * @since 2020-08-14
 */
@Service
public class SysShortMessageServiceImpl extends ServiceImpl<SysShortMessageMapper, SysShortMessage> implements SysShortMessageService {
	
	@Override
    public JSONObject sendShortMessage(String signName, String phoneNumber
            , String templateCode, JSONObject templateParam) throws Exception {
        JSONObject obj = new JSONObject();
        if(StringUtils.isBlank(signName)){
            obj.put("code","1001");
            obj.put("codeDesc","签名为空!");
            return obj;
        }
        if(StringUtils.isBlank(phoneNumber)){
            obj.put("code","1002");
            obj.put("codeDesc","手机号为空!");
            return obj;
        }
        if(StringUtils.isBlank(templateCode)){
            obj.put("code","1003");
            obj.put("codeDesc","模板为空!");
            return obj;
        }
        if(null==templateParam){
            obj.put("code","1004");
            obj.put("codeDesc","短信参数为空!");
            return obj;
        }
        //发送短信
        DefaultProfile profile = DefaultProfile
                .getProfile("cn-hangzhou", "LTAII0t04Yk8eIcw", "b0NsKygG6DzAqPHZS90gPZYr89yOHZ");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam.toString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            String resultInfo = response.getData();
            if(StringUtils.isBlank(resultInfo)){
                obj.put("code","9999");
                obj.put("codeDesc","发送失败!");
                return obj;
            }
            System.out.println(resultInfo);
            JSONObject resultJson = JSONObject.fromObject(resultInfo);
            if(null==resultJson){
                obj.put("code","9999");
                obj.put("codeDesc","发送失败!");
                return obj;
            }
            if(resultJson.containsKey("Code")){
                String messageCode =resultJson.getString("Code");
                if("OK".equals(messageCode)){
                    obj.put("code","0000");
                    obj.put("codeDesc","发送成功!");
                    return obj;
                }
            }else {
                obj.put("code","9999");
                obj.put("codeDesc","发送失败!");
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	 
	@Override
    public JSONObject checkVerifyCode(String phoneNumber, String verifyCode, HttpServletRequest request) throws Exception {
        JSONObject resultMap = new JSONObject();
        if("".equals(Util.Null2EmptyTrim(verifyCode))){
            resultMap.put("code","0001");
            resultMap.put("codeDesc","验证码为空！");
            return resultMap;
        }
        if("".equals(Util.Null2EmptyTrim(phoneNumber))){
            resultMap.put("code","0002");
            resultMap.put("codeDesc","手机号为空！");
            return resultMap;
        }
        String serviceCode = (String) request.getSession().getAttribute("verifyCode");
        String servicePhone = (String) request.getSession().getAttribute("verifyPhone");
        if(!verifyCode.equals(serviceCode)) {
        	 resultMap.put("code","0003");
             resultMap.put("codeDesc","验证码错误！");
             return resultMap;
        }
        
        if(!phoneNumber.equals(servicePhone)) {
       	 	resultMap.put("code","0004");
            resultMap.put("codeDesc","输入手机号非接收验证码的手机号！");
            return resultMap;
       }
              
	    resultMap.put("code","0000");
	    resultMap.put("codeDesc","验证成功！");
	    return resultMap;      
    }
	
	

}
